package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.ruoyi.system.domain.PetSupply;
import com.ruoyi.system.domain.ServiceItem;
import com.ruoyi.system.domain.Vo.ReservationVo;
import com.ruoyi.system.service.IPetSupplyService;
import com.ruoyi.system.service.IServiceItemService;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Reservation;
import com.ruoyi.system.service.IReservationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 服务预约信息Controller
 * 
 * @author ruoyi
 * @date 2025-02-18
 */
@Controller
@RequestMapping("/system/reservation")
public class ReservationController extends BaseController
{
    private String prefix = "system/reservation";

    @Resource
    private IPetSupplyService petSupplyService;

    @Autowired
    private IReservationService reservationService;

    @Resource
    private IServiceItemService serviceItemService;

    @Resource
    private ISysRoleService roleService;

    @RequiresPermissions("system:reservation:view")
    @GetMapping()
    public String reservation()
    {
        return prefix + "/reservation";
    }

    /**
     * 查询服务预约信息列表
     */
    @RequiresPermissions("system:reservation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        List<ReservationVo> reservationVos = new ArrayList<>();
        list.forEach(item -> {
            ServiceItem serviceItem = serviceItemService.selectServiceItemById(item.getSupplyId());
            ReservationVo reservationVo = new ReservationVo();
            BeanUtils.copyProperties(item, reservationVo);
            reservationVo.setSupplyName(serviceItem.getName());
            reservationVos.add(reservationVo);
        });
        return getDataTable(reservationVos);
    }

    /**
     * 导出服务预约信息列表
     */
    @RequiresPermissions("system:reservation:export")
    @Log(title = "服务预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        return util.exportExcel(list, "服务预约信息数据");
    }

    /**
     * 新增服务预约信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("petSupplyList",serviceItemService.selectServiceItemList(new ServiceItem()));
        return prefix + "/add";
    }

    /**
     * 新增保存服务预约信息
     */
    @RequiresPermissions("system:reservation:add")
    @Log(title = "服务预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改服务预约信息
     */
    @RequiresPermissions("system:reservation:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Reservation reservation = reservationService.selectReservationById(id);
        mmap.put("reservation", reservation);
        mmap.put("petSupplyList",serviceItemService.selectServiceItemList(new ServiceItem()));
        return prefix + "/edit";
    }

    /**
     * 修改保存服务预约信息
     */
    @RequiresPermissions("system:reservation:edit")
    @Log(title = "服务预约信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除服务预约信息
     */
    @RequiresPermissions("system:reservation:remove")
    @Log(title = "服务预约信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }
}
